<?php
//活动管理

namespace App\Http\Controllers\Manage;

use App\Common\CommonConst;
use App\Http\Requests\Manage\Activity\SaveRequest;
use App\Services\Manage\ActivityManageService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;


class ActivityController extends BaseController
{

    /**
     * 活动列表
     * @param  Request  $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 20);
        $titleKeyword = $request->get('title_keyword', ''); //活动名称
        $locationKeyword = $request->get('location_keyword', ''); //活动地点
        $activityStatus = $request->get('activity_status', 0); //活动状态：0.全部；1.草稿；2.未开始；3.进行中；4.已结束；
        $receiveDateStart = $request->get('receive_date_start', 0); //领取时间-开始
        $receiveDateEnd = $request->get('receive_date_end', 0); //领取时间-结束

        $res = ActivityManageService::list([
            'title_keyword'    => $titleKeyword,
            'location_keyword'    => $locationKeyword,
            'activity_status'  => $activityStatus,
            'receive_date_start' => $receiveDateStart,
            'receive_date_end'   => $receiveDateEnd,
        ], $page, $pageSize);

        return $this->success($res);
    }

    /**
     * 编辑回显信息
     * @param  Request  $request
     * @return JsonResponse
     */
    public function editShow(Request $request)
    {
        $id = $request->get('id', 0);

        $res = ActivityManageService::editShow($id);

        return $this->success($res);
    }

    /**
     * 编辑保存
     * @param  Request  $request
     * @return JsonResponse
     */
    public function editSave(SaveRequest $request)
    {
        $data = $request->all();

        $res = ActivityManageService::editSave($data);
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success([], $res['errmsg']);
    }

    /**
     * 删除活动
     * @param  Request  $request
     * @return JsonResponse
     */
    public function del(Request $request)
    {
        $activityId = $request->post('id', 0);

        $res = ActivityManageService::del($activityId);
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success([], $res['errmsg']);
    }

}
